<template>
  <div class="u-p-20">
    <el-card shadow="never">
      <div slot="header">
        <span>
          <b class="u-f-16">商品列表</b>
        </span>
        <router-link :to="{ name: 'goodsEdit' }" v-if="hasPerm('goodsEdit')">
          <el-button type="danger" size="small" icon="el-icon-plus" class="u-right u-5mt" round>添加商品</el-button>
        </router-link>
      </div>

      <el-form :inline="true" :model="queryForm" size="small" class="u-text-center">
        <el-form-item label="商品分类">
          <el-cascader :options="goodstypeList" @change="handleLevelSelect" placeholder="选择分类" clearable
            :props="levelProps" v-model="goodsType">
            <template slot-scope="{ node, data }">
              <span>{{ data.name }}</span>
              <span v-if="!node.isLeaf">
                ({{ data.children.length }})
              </span>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="上线状态">
          <el-select v-model="queryForm.status" clearable @change="handleChange">
            <el-option label="全部" value></el-option>
            <el-option label="上线" value="1"></el-option>
            <el-option label="下线" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="queryForm.keyword" placeholder="输入商品标题查询" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getPageData(true)">查询</el-button>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="success" size="mini" @click="handleStatusIds(1)">批量上架</el-button>
        <el-button type="danger" size="mini" @click="handleStatusIds(2)">批量下架</el-button>
        <!-- <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="调整比例">
            <el-input v-model="ratio" size="mini" placeholder="价格调整比例"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="modPrice">修改</el-button>
          </el-form-item>
        </el-form> -->
      </div>
      <el-table :data="tableData" class="u-mt-10" v-viewer @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="id" prop="id" width="50"></el-table-column>
        <el-table-column label="图片" width="90">
          <template slot-scope="scope">
            <el-image style="max-height:90px;max-width: 90px;" :src="scope.row.goods_img"
              :data-src="scope.row.goods_img" fit="cover" lazy></el-image>
          </template>
        </el-table-column>

        <el-table-column label="商品分类" prop="goods_type_name" width="100"></el-table-column>
        <el-table-column label="标题" prop="goods_title" width="140"></el-table-column>
        <!-- <el-table-column label="运费" width="150">
          <template slot-scope="scope">
            <div>{{ scope.row.freight_info.name }}</div>
          </template>
        </el-table-column> -->
        <el-table-column label="商品详情" width="620">
          <template slot-scope="scope">
            <el-table :data="scope.row.item_show_list" class="item-list">
              <el-table-column prop="attribute_names" label="规格"></el-table-column>
              <el-table-column prop="goods_unit_price" label="单价"></el-table-column>
              <el-table-column prop="goods_cost_price" label="进货价"></el-table-column>
              <el-table-column prop="goods_sell_price" label="售价"></el-table-column>
              <!-- <el-table-column label="进货价">
                <template slot-scope="scopes">
                  <el-input size="mini" v-model="scopes.row.goods_cost_price" @blur="onCostPrice(scopes.row)">
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column label="售价" width="100">
                <template slot-scope="scopes">
                  <el-input size="mini" v-model="scopes.row.goods_sell_price" @blur="onSellPrice(scopes.row)">
                  </el-input>
                </template>
              </el-table-column> -->
              <el-table-column prop="difference_price" label="总利润"></el-table-column>
              <el-table-column prop="goods_ratio_price" label="分成量"></el-table-column>
              <el-table-column prop="goods_storage" label="库存"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" @click.native="dialogOpen(scope.row)">库存管理</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="goods-block-control" @click="moreChange(scope.$index)" v-if="scope.row.item_list_len > 3">
              <i :class="'el-icon-caret-' + (scope.row.more ? 'top' : 'bottom')"></i>
              <span>{{ scope.row.more ? '收起列表' : '显示更多' }}</span>
            </div>
            <div class="goods-sales" v-if="false">
              <el-tag class="goods-sales-item">群主提成{{ scope.row.goods_return_agency_money }}%</el-tag>
              <el-tag class="goods-sales-item">一级分销{{ scope.row.goods_top_money }}%</el-tag>
              <el-tag class="goods-sales-item">二级分销{{ scope.row.goods_top_top_money }}%</el-tag>
              <el-tag class="goods-sales-item">用户分享{{ scope.row.goods_channel_money }}%</el-tag>
              <el-tag class="goods-sales-item">用户返现{{ scope.row.goods_return_money }}%</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品状态" width="80">
          <template slot-scope="scope">
            <div v-if="hasPerm('goodsStatus') && scope.row.goods_status < 3">
              <el-switch v-model="scope.row.goods_status" :active-value="1" :inactive-value="2" active-color="#13ce66"
                @change="handleStatus(scope.row.id, scope.row.goods_status)"></el-switch>
            </div>
            <div v-if="scope.row.goods_status > 2">
              <el-tag v-if="scope.row.goods_status == 3">审核中</el-tag>
              <el-tag v-if="scope.row.goods_status == 4">审核失败</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否预售" width="80">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.goods_reserve == 0">否</el-tag>
            <el-tag type="success" v-if="scope.row.goods_reserve == 1">是</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" width="80"></el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click.native="dialogListOpen(scope.row.id)">库存记录</el-button>
            <el-button size="mini" type="text" @click.native="handleRoute(scope.row.id, 'goodsEdit')"
              v-if="hasPerm('goodsEdit')">编辑</el-button>
            <el-button size="mini" type="text" @click.native="handleDelete(scope.row.id)" v-if="hasPerm('goodsDelBtn')">
              删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="u-mt-20 u-text-center">
        <el-pagination background @size-change="handleSizeChange" @current-change="handlePageChange"
          :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50]" :page-size="limit"
          layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
      </div>
    </el-card>
    <el-dialog title="平台入库" :visible.sync="is_goods_show" width="600px">
      <el-table :data="form.goods">
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="attribute_names" label="商品规格"></el-table-column>
        <el-table-column prop="goods_storage" label="库存"></el-table-column>
      </el-table>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作状态" style="margin-top: 15px">
              <el-radio v-model="form.change" label="add">入库</el-radio>
              <el-radio v-model="form.change" label="sub">出库</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="渠道商" style="margin-top: 15px">
              <el-select v-model="form.target_id" placeholder="请选择渠道商">
                <el-option label="无渠道商" value="0"></el-option>
                <el-option v-for="(item, index) in distributor_list" :key="item.id + index" :label="item.name"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库数量">
              <el-input v-model="form.storage" type="number" placeholder="请输入库数量"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注信息">
              <el-input type="text" v-model="form.remark"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="is_goods_show = false">取 消</el-button>
        <el-button type="primary" @click="submitStore">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 商品弹出框 -->
    <el-dialog title="库存记录" :visible.sync="is_list_show" width="1200px">
      <Storelog :pid="pid" operate="store"></Storelog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogListClose()">取 消</el-button>
        <el-button type="primary" @click="dialogListClose()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import commonPage from "@/mixins/common-page";
import Storelog from "@/views/goods/storelog.vue";

export default {
  mixins: [commonPage],
  components: {
    Storelog
  },
  props: {},
  data() {
    return {
      apiName: "goods",
      apiNameStore: "goodsStore",
      limit: 10,
      goodsType: 0,
      ratio: 1,//比例
      price: 0,//价格
      queryForm: {
        status: "",
        keyword: "",
        goodsType: "",
        check: "item",
      },
      levelProps: {
        label: "name",
        value: "id",
        children: "children",
      },
      distributor_list: [],
      form: {
        id: "",
        change: "add",
        attribute_ids: "",
        storage: 0,
        target_id: "",
        remark: "",
        goods: [],
      },
      pid: "",
      is_goods_show: false,
      is_list_show: false,
      goodstypeList: [],
      idArray: [], // 批量关闭
    };
  },
  methods: {
    moreChange(index) {
      let more = this.tableData[index].more;
      if (more) {
        this.tableData[index].item_show_list = this.tableData[
          index
        ].item_list.slice(0, 3);
      } else {
        this.tableData[index].item_show_list = this.tableData[index].item_list;
      }
      this.tableData[index].more = !more;
    },
    onSellPrice(val) {
      let self = this;
      let json = {
        id: val.id,
        price: val.goods_sell_price,
        type: "sell"
      };
      self.$api[self.apiName].mod_com_price(json).then((response) => {
        if (response.code == 1) {
          self.$message({
            message: "操作成功",
            type: "success",
          });

        }
      });
    },
    onCostPrice(val) {
      // console.log(val.id);
      // console.log(val.goods_cost_price);
      let self = this;
      let json = {
        id: val.id,
        price: val.goods_cost_price,
        type: "cost"
      };
      self.$api[self.apiName].mod_com_price(json).then((response) => {
        if (response.code == 1) {
          self.$message({
            message: "操作成功",
            type: "success",
          });

        }
      });
    },
    modPrice() {
      let self = this;
      if (this.goodsType) {
        let json = {
          type_id: this.goodsType[this.goodsType.length - 1],
          ratio: this.ratio
        };
        self.$api[self.apiName].mod_price(json).then((response) => {
          if (response.code == 1) {
            self.$message({
              message: "操作成功",
              type: "success",
            });
            this.getPageData(true);
          }
        });
      } else {
        self.$message({
          message: "请选择商品分类",
          type: "error",
        });
      }

    },
    handleLevelSelect(val) {
      if (!(val == null || val == undefined)) {
        this.queryForm.goodsType = val[val.length - 1];
        this.getPageData(true);
      }
    },

    handleChange() {
      this.queryForm.goodsType = this.goodsType[1];
      this.getPageData(true);
    },
    dialogOpen(item) {
      this.form.goods[0] = item;
      this.form.id = item.goods_id;
      this.form.attribute_ids = item.attribute_ids;
      this.storage = 0;
      this.target_id = "";
      this.remark = "";
      this.is_goods_show = true;
    },
    dialogListOpen(id) {
      this.is_list_show = true;
      this.pid = id + "";
    },
    dialogListClose() {
      this.is_list_show = false;
      this.pid = "";
    },
    submitStore() {
      let self = this;
      if (self.form.id) {
        self.$api[self.apiNameStore].save(self.form).then((response) => {
          if (response.code == 1) {
            self.$message({
              message: "操作成功",
              type: "success",
            });
          }
          self.is_goods_show = false;
          self.onLoad();
        });
      }
      this.pid = "";
    },
    getLoadList() {
      this.$api[this.apiName].load_list().then((response) => {
        if (response.code == 1) {
          this.distributor_list = response.data.distributorList;
          this.goodstypeList = response.data.levelTree;
          this.freightTemplate = response.data.freightTemplate;
          this.freightTemplate.push({
            id: 0,
            name: "未选择",
          });
        }
      });
    },
    handleSelectionChange(val) {
      val.map((item) => {
        this.idArray.push(item.id);
      });
    },
    handleStatusIds(status) {
      //status 1上架，2下架
      let str = "上架";
      let type_str = "success";
      if (parseInt(status) == 2) {
        str = "下架";
        type_str = "warning";
      }
      if (this.idArray == "") {
        this.$message({
          type: "error",
          message: "请选择要操作的数据!",
        });
      } else {
        this.$confirm("确认" + str + "吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: type_str,
        })
          .then(() => {
            this.$api[this.apiName]
              .status(this.idArray, status)
              .then((response) => {
                if (response.code == 1) {
                  this.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                  this.onLoad();
                }
              });
          })
          .catch(() => { });
      }
    },
  },
  mounted() {
    this.getLoadList();
  },
};
</script>
<style >
.goods-block-control {
  box-sizing: border-box;
  background-color: #fff;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  text-align: right;
  cursor: pointer;
  padding: 5px 0;
  position: relative;
}

.goods-block-control span {
  margin-right: 15px;
}

.goods-block-control i {
  margin-right: 5px;
}

.goods-sales .goods-sales-item {
  /* display: inline-block; */
  margin-left: 5px;
}

.goods-item-list>view {
  display: inline-block;
}

.price-label {
  display: inline-block;
  width: 90px;
}

.price-price {
  display: inline-block;
}
</style>